#include "../server.hpp"

class EchoServer
{
public:
    EchoServer(int port) : _server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
    }
    void Start()
    {
        _server.Start();
    }

private:
    void OnMessage(const Connection::PtrConnection &conn, Buffer *buf)
    {
        buf->MoveReadOffset(buf->ReadableSize());
        std::string str = "hello world";
        conn->Send(str.c_str(), str.size());
    }
    void OnClosed(const Connection::PtrConnection &conn)
    {
        LOG(LogLevel::INFO) << "connection " << conn->Id() << " closed";
    }
    void OnConnected(const Connection::PtrConnection &conn)
    {
        LOG(LogLevel::INFO) << "new connection " << conn->Id();
    }

private:
    TcpServer _server;
};